package com.icesoft.system.starter;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONArray;
import com.icesoft.framework.core.config.Cons;
import com.icesoft.framework.core.util.PathUtil;
import com.icesoft.system.entity.*;
import com.icesoft.system.service.impl.*;
import com.icesoft.system.types.AuthTypeDictionary;
import com.icesoft.system.types.RoleTypeDictionary;
import com.icesoft.system.types.SystemSettingTypeDictionary;
import com.icesoft.system.types.UserTypeDictionary;
import com.icesoft.system.vo.MenuVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Slf4j
@Component
@Order
@RequiredArgsConstructor
public class SystemAutoInitializer implements ApplicationRunner {

	private final MenuServiceImpl menuService;

	private final UserServiceImpl userService;

	private final AccountServiceImpl accountService;

	private final RoleServiceImpl roleService;

	private final UserRoleServiceImpl userRoleService;

	private final PermissionServiceImpl authorityService;

	private final RoleMenuServiceImpl roleMenuService;

	private final RoleTypeDictionary roleTypeDictionary;

	private final UserTypeDictionary userTypeDictionary;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		insertInitData();
	}

	private void insertInitData() throws IOException {

		if (menuService.count() == 0) {
			String json = IoUtil.readUtf8(PathUtil.getPathInputStream("META-INF/json/menu.json"));
			List<MenuVO> sysMenuList = JSONArray.parseArray(json, MenuVO.class);
			for (MenuVO tab : sysMenuList) {
				tab.setPid(0L);
				menuService.createEntity(tab);
				if (tab.getChildren() != null && !tab.getChildren().isEmpty()) {
					for (MenuVO menu : tab.getChildren()) {
						menu.setPid(tab.getId());
						menuService.createEntity(menu);
					}
				}
			}
		}

		if (userService.count() == 0) {
			User user = new User();
			user.setId(1L);
			user.setPhone("18099106457");
			user.setEmail("18099106457@163.com");
			user.setRealName("系统管理员");
			user.setCode("ADMIN");
			user.setOrgId(1L);
			user.setUserType(userTypeDictionary.getSystem());
			user.setCompanyId(1L);
			userService.createEntity(user);

			accountService.createSystemEntity(user, "admin");

			if (roleService.count() == 0) {
				Role role = new Role();
				role.setName("系统管理员角色");
				role.setOrgId(1L);
				role.setCode(Cons.ROLE_SUPER_ADMIN);
				role.setRoleType(roleTypeDictionary.getAll());
				roleService.createEntity(role);

				List<Menu> list = menuService.list();
				for (Menu menu : list) {
					roleMenuService.createEntity(new RoleMenu(role.getId(), menu.getId()));
				}
				userRoleService.createEntity(new UserRole(user.getId(), role.getId()));

				Permission authority = new Permission();
				authority.setRoleId(role.getId());
				authority.setAuthority("/**");
				authorityService.createEntity(authority);
			}

		}

	}
}
